package cn.itsource.blog.service.impl;

import cn.itsource.blog.domain.ArticleType;

import cn.itsource.blog.mapper.ArticleTypeMapper;
import cn.itsource.blog.query.ArticleTypeQuery;
import cn.itsource.blog.service.IArticleTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ArticleTypeServiceImpl implements IArticleTypeService {

    @Autowired
    private ArticleTypeMapper articleTypeMapper;

    // 新增文章类型
    @Override
    @Transactional
    @CacheEvict(cacheNames = "articleType",key = "'typeTree'") // 配合springCache使用redis，删除缓存数据
    public void add(ArticleType articleType) {
        articleTypeMapper.add(articleType);
    }

    // 删除文章类型
    @Override
    @Transactional
    @CacheEvict(cacheNames = "articleType",key = "'typeTree'")
    public void delete(Long id) {
        articleTypeMapper.delete(id);
    }

    // 查询单个文章类型
    @Override
    public ArticleType get(Long id) {
        return articleTypeMapper.get(id);
    }

    // 查询所有的文章类型
    @Override
    public List<ArticleType> getAll() {
        return articleTypeMapper.getAll();
    }

    // 批量删除文章类型
    @Override
    @Transactional
    @CacheEvict(cacheNames = "articleType",key = "'typeTree'")
    public void delPatch(Long[] ids) {
        articleTypeMapper.delPatch(ids);
    }

    // 使用分页插件进行分页查询
    @Override
    @Transactional
    public PageInfo<ArticleType> pageInfo(ArticleTypeQuery articleTypeQuery) {
        System.out.println(articleTypeQuery);
        // 设置参数
        PageHelper.startPage(articleTypeQuery.getCurrentPage(), articleTypeQuery.getPageSize());
        // 查询用户信息
        List<ArticleType> articleTypes = articleTypeMapper.getAllByQuery(articleTypeQuery);
        // 返回查询结果
        return new PageInfo<>(articleTypes);
    }

    // 更新文章类型
    @Override
    @Transactional
    @CacheEvict(cacheNames = "articleType",key = "'typeTree'")
    public void updata(ArticleType articleType) {
        articleTypeMapper.update(articleType);
    }

    // 获取文章类型树，如果缓存有走缓存，没有走数据库查询数据并将数据放入缓存中
    @Override
    @Transactional
    @Cacheable(cacheNames = "articleType",key = "'typeTree'") // 配合springCache使用redis，缓存数据
    public List<ArticleType> getTypeTree() {
            return getTypeTreeByDb();
    }

    // 获取类型树的数据
    public List<ArticleType> getTypeTreeByDb() {
        List<ArticleType> list = articleTypeMapper.getAll();
        Map<Long, ArticleType> map = list.stream().collect(Collectors.toMap(ArticleType::getId, r -> r));
        List<ArticleType> articleTypes = new ArrayList<>();
        for (ArticleType a : list) {
            if (a.getParentId() == null) {
                articleTypes.add(a);
            } else {
                ArticleType articleType = map.get(a.getParentId());
                if (articleType != null) {
                    articleType.getChildren().add(a);
                }
            }
        }
        return articleTypes;
    }
}
